﻿using System;
using System.Linq;
using System.ServiceProcess;
using System.Threading;
using DataProcessor.DB.ORM;
using Hjt212Parser;
using log4net;

namespace DataProcessor
{
    public partial class ServiceDataProcessor : ServiceBase
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(ServiceDataProcessor));
        public ServiceDataProcessor()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            try
            {
                new Thread(ProcessDataMsg) { IsBackground = true }.Start();
                Log.Info("完成启动服务...");
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
        }

        protected override void OnStop()
        {
            try
            {
                Log.Warn("服务停止!");
                Environment.Exit(0);
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
        }

        private void ProcessDataMsg(object state)
        {
            while (true)
            {
                try
                {
                    using (var gCtx=new GatewayEntities())
                    {
                        var notProcessed = "NotProcessed";
                        var items =
                            gCtx.received_msg.Where(t => t.is_length_right)
                                .Where(t => t.processed_flag.Contains(notProcessed))
                                .OrderBy(t => t.datatime)
                                .Take(1000)
                                .ToList();
                        foreach (var item in items)
                        {
                            Log.Info("Processing:" + Environment.NewLine + item.received_string);
                          
                            switch (item.cn)
                            {
                                case "2011":
                                case "G011":
                                    //DataInstructionProcessor.Process2011(item.received_string);
                                    break;
                                case "2051":
                                case "G051":
                                    //DataInstructionProcessor.Process2051(item.received_string);
                                    break;
                                case "2061":
                                case "G061":
                                    //DataInstructionProcessor.Process2061(item.received_string);
                                    //DataInstructionProcessor.ProcessHourToDay(item.received_string);
                                    //DataInstructionProcessor.ProcessDayToMonth(item.received_string);
                                    break;
                                case "2031":
                                case "G031":
                                    //DataInstructionProcessor.Process2031(item.received_string);
                                    //DataInstructionProcessor.ProcessDayToMonth(item.received_string);
                                    //DataInstructionProcessor.ProcessMonthToYear(item.received_string);
                                    break;
                                case "9011":
                                    break;
                                case "9012":
                                    break;
                                case "9014":
                                    break;
                                case "3097":
                                    break;
                                case "3071":
                                    break;

                            }
                            item.processed_flag = "ProcessingFinished";
                            Log.Info("Processing finish.");
                        }
                        gCtx.SaveChanges();
                      
                    }
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                }
                ThreadUtil.Sleep(100);
            }
        }
    }
}
